#!/bin/bash

if [ $# -lt 2 ]; then
    echo "Usage: $0 DIST COMPONENT [username]"
    exit 1
fi

set -e
shopt -s nullglob
[ "$DEBUG" = "1" ] && set -x

DIST=$1
COMPONENT=$2
RUN_AS_USER="user"
if [ $# -gt 2 ]; then
    RUN_AS_USER=$3
fi

: "${MAKE_TARGET=rpms}"

if [ ${DIST#fc} -ge 25 ]; then
    YUM=dnf
else
    YUM=yum
fi

ORIG_SRC=$PWD/qubes-src/$COMPONENT
DIST_SRC_ROOT=$PWD/chroot-dom0-$DIST/home/user/qubes-src/
DIST_SRC=$DIST_SRC_ROOT/$COMPONENT
BUILDER_REPO_DIR=$PWD/qubes-packages-mirror-repo/dom0-$DIST

MAKE_TARGET_ONLY="${MAKE_TARGET/ */}"
REQ_PACKAGES="build-pkgs-$COMPONENT.list"
[ -r "build-pkgs-$COMPONENT-$MAKE_TARGET_ONLY.list" ] && REQ_PACKAGES="build-pkgs-$COMPONENT-$MAKE_TARGET_ONLY.list"
[ -r "$ORIG_SRC/build-deps.list" ] && REQ_PACKAGES="$ORIG_SRC/build-deps.list"
[ -r "$ORIG_SRC/build-deps-$MAKE_TARGET_ONLY.list" ] && REQ_PACKAGES="$ORIG_SRC/build-deps-$MAKE_TARGET_ONLY.list"

export USER_UID=$UID
if ! [ -e "chroot-dom0-$DIST/home/user/.prepared_base" ]; then
    make --no-print-directory \
        DIST=$DIST \
        PACKAGE_SET=dom0 \
        COMPONENT=builder-rpm \
        USE_DIST_BUILD_TOOLS=0 \
        -f Makefile.generic prepare-chroot || exit 1;
fi

sudo mount --bind "$BUILDER_REPO_DIR" "$PWD/chroot-dom0-$DIST/tmp/qubes-packages-mirror-repo"
BUILDER_REPO_DIR="$BUILDER_REPO_DIR" $PWD/qubes-src/builder-rpm/update-local-repo.sh "$DIST"
sudo chroot "$PWD/chroot-dom0-$DIST" $YUM $YUM_OPTS update -y
if [ -r "$REQ_PACKAGES" ] && [ "$REQ_PACKAGES" -nt "chroot-dom0-$DIST/home/user/.installed_${COMPONENT}_$(basename "$REQ_PACKAGES")" ]; then
    sed "s/DIST/$DIST/g" "$REQ_PACKAGES" > build-pkgs-temp.list
    echo "-> Installing $COMPONENT build dependencies in $DIST environment"
    TMP_PACKAGES_ARR=( $(cat build-pkgs-temp.list) )

    if [ "x${FEDORA_MIRROR}" != "x" ]; then
        YUM_OPTS="$YUM_OPTS --setopt=fedora.baseurl=${FEDORA_MIRROR%/}/releases/${DIST#fc}/Everything/x86_64/os/"
        YUM_OPTS="$YUM_OPTS --setopt=updates.baseurl=${FEDORA_MIRROR%/}/updates/${DIST#fc}/Everything/x86_64/"
    fi

    sudo chroot "$PWD/chroot-dom0-$DIST" $YUM $YUM_OPTS install -y "${TMP_PACKAGES_ARR[@]}"
    rm -f build-pkgs-temp.list
    touch "chroot-dom0-$DIST/home/user/.installed_${COMPONENT}_$(basename "$REQ_PACKAGES")"
fi
sudo umount "$PWD/chroot-dom0-$DIST/tmp/qubes-packages-mirror-repo"

if ! [ -r "$PWD/chroot-dom0-$DIST/proc/cpuinfo" ]; then
    sudo mount -t proc proc "$PWD/chroot-dom0-$DIST/proc"
fi

if ! [ -d "$PWD/chroot-dom0-$DIST/sys/devices" ]; then
    sudo mount -t sysfs sysfs "$PWD/chroot-dom0-$DIST/sys"
fi

mkdir -p "$DIST_SRC_ROOT"
sudo rm -rf "$DIST_SRC"
cp -alt "$DIST_SRC_ROOT" "$ORIG_SRC"
rm -rf "$DIST_SRC"/rpm/{x86_64,i686,noarch,SOURCES}
# shellcheck disable=SC1090
[ -x "$ORIG_SRC/qubes-builder-pre-hook.sh" ] && source "$ORIG_SRC/qubes-builder-pre-hook.sh"
# Disable rpm signing in chroot - there are no signing keys
sed -i -e 's/rpm --addsign/@true \0/' "$DIST_SRC"/Makefile*

BUILD_INITIAL_INFO="-> Building $COMPONENT $MAKE_TARGET_ONLY for $DIST"
BUILD_LOG=
if [ "$VERBOSE" -eq 0 ]; then
    BUILD_LOG="build-logs/$COMPONENT-$MAKE_TARGET_ONLY-$DIST.log"
    if [ -e "$BUILD_LOG" ]; then
	mv -f "$BUILD_LOG" "$BUILD_LOG.old"
    fi
    BUILD_INITIAL_INFO="$BUILD_INITIAL_INFO (logfile: $BUILD_LOG)..."
fi
echo "$BUILD_INITIAL_INFO"
if [ "$VERBOSE" -ge 1 ]; then
    sed -i -e 's/rpmbuild/rpmbuild --quiet/' "$DIST_SRC"/Makefile*
    MAKE_OPTS="$MAKE_OPTS -s"
fi
# shellcheck disable=SC1090
[ -x "$ORIG_SRC/qubes-builder-pre-hook.sh" ] && source "$ORIG_SRC/qubes-builder-pre-hook.sh"
set +e
MAKE_CMD="cd /home/user/qubes-src/$COMPONENT; NO_SIGN='$NO_SIGN' make $MAKE_OPTS $MAKE_TARGET"
if [ "$VERBOSE" -eq 0 ]; then
    # shellcheck disable=SC2024
    sudo -E chroot "chroot-dom0-$DIST" su -s /bin/bash -p -c "$MAKE_CMD" "$RUN_AS_USER" >"$BUILD_LOG" 2>&1
    BUILD_RETCODE=$?
else
    sudo -E chroot "chroot-dom0-$DIST" su -s /bin/bash -p -c "$MAKE_CMD" "$RUN_AS_USER"
    BUILD_RETCODE=$?
fi
if [ $BUILD_RETCODE -gt 0 ]; then
    echo "--> build failed!"
    if [ -n "$BUILD_LOG" ]; then
        tail "$BUILD_LOG"
    fi
    exit 1
fi
set -e
# shellcheck disable=SC1090
[ -x "$ORIG_SRC/qubes-builder-post-hook.sh" ] && source "$ORIG_SRC/qubes-builder-post-hook.sh"
echo "--> Done:"
for i in "$DIST_SRC"/rpm/*; do
    ARCH_RPM_DIR="$ORIG_SRC/rpm/$(basename "$i")"
    mkdir -p "$ARCH_RPM_DIR"
    for pkg in $i/*; do
        echo "     qubes-src/$COMPONENT/rpm/$(basename "$i")/$(basename "$pkg")"
    done
    mkdir -p "$BUILDER_REPO_DIR/rpm"
    ln -f -t "$BUILDER_REPO_DIR/rpm" "$i/"*
    mv -t "$ARCH_RPM_DIR" "$i/"*
done
if [ "$COMPONENT" == "$INSTALLER_COMPONENT" ]; then
    if [ "$MAKE_TARGET_ONLY" == "iso" ]; then
        if [ -d "$DIST_SRC/build/work" ]; then
            sudo rm -fr "$ORIG_SRC/build/ISO"
            sudo rm -fr "$ORIG_SRC/build/work"
            mkdir -p "$ORIG_SRC/build/"
            sudo mv "$DIST_SRC/build/work" "$ORIG_SRC/build/"
            sudo mv "$DIST_SRC/build/ISO" "$ORIG_SRC/build/"
        fi
    fi
fi
